package com.silverhaze.diamond.client.utils.db.dev;

import com.silverhaze.diamond.client.utils.db.ServiceDataSource;
import com.silverhaze.diamond.common.entity.*;

import java.sql.SQLException;
import java.util.Date;
import java.util.Random;

/**
 * Created by Ivan Babanin (13:39:12 09.06.2008)
 * This part of code is strictly confidential.
 * © Magnificent Software Inc.
 */
public class DBInitializer {
    public static void main(String[] args) throws SQLException {
        initAdministrator();
//        generateDetails(10000);
//        generateCustomers(10000);
//        generateServices(5000);
    }

//    private static void generateServices(int count) {
//        for (int i = 0; i < count; i++) {
//            Service service = new Service();
//
//            service.setSystemUser(DAOLocator.getInstance().getUsers().get(0));
//            service.setDateProduce(new Date());
//            service.setTechNames(generateSomeString());
//            service.setTechModelName(generateSomeString());
//            service.setStampState(generateSomeString());
//            service.setSerialNumber(generateSomeString());
//            service.setOwnerDefect(generateSomeString());
//            service.setMasterDefect(generateSomeString());
//            service.setOwner(DAOLocator.getInstance().getCustomers().get(generateSomeNumber(100)));
//            service.setModelName(generateSomeString());
//            service.setGuarantyId(generateSomeString());
//            service.setCompleteness(generateSomeString());
//
//            DAOLocator.getInstance().add(service);
//
//            if (generateSomeNumber(1000) % 2 == 0) {
//                service.setCost(generateSomeNumber(10000));
//                Set<BrokenDetail> detailSet = generateSomeBrokenDetails();
//                service.setBrokenDetailes(detailSet);
//                Set<UsedDetail> detailSet1 = generateSomeUsedDetails();
//                service.setUsedDetailes(detailSet1);
//                service.setServiceCompleteDate(new Date());
//                service.setServiceComplete(true);
//
//                DAOLocator.getInstance().update(service);
//                ServiceDataSource.getInstance().updateService(service);
//            }
//        }
//    }
//
//    private static Set<UsedDetail> generateSomeUsedDetails() {
//        int size = generateSomeNumber(50);
//        Set<UsedDetail> usedDetails = new HashSet<UsedDetail>();
//
//        for (int i = 0; i < size; i++) {
//            UsedDetail usedDetail = new UsedDetail();
//            usedDetail.setDetail(DAOLocator.getInstance().getDetails().get(generateSomeNumber(DAOLocator.getInstance().getDetails().size())));
//            usedDetail.setQuantity(generateSomeNumber(100));
//
//            usedDetails.add(usedDetail);
//        }
//
//        DAOLocator.addEntities(usedDetails);
//
//        return usedDetails;
//    }
//
//    private static Set<BrokenDetail> generateSomeBrokenDetails() {
//        int size = generateSomeNumber(50);
//        Set<BrokenDetail> brokenDetails = new HashSet<BrokenDetail>();
//
//        for (int i = 0; i < size; i++) {
//            BrokenDetail brokenDetail = new BrokenDetail();
//            brokenDetail.setDetail(DAOLocator.getInstance().getDetails().get(generateSomeNumber(DAOLocator.getInstance().getDetails().size())));
//            brokenDetail.setQuantity(generateSomeNumber(100));
//
//            brokenDetails.add(brokenDetail);
//        }
//
//        DAOLocator.addEntities(brokenDetails);
//
//        return brokenDetails;
//    }

    private static void generateCustomers(int count) {
        for (int i = 0; i < count; i++) {
            Customer customer = new Customer();
            customer.setName(generateSomeString());
            customer.setPatronymic(generateSomeString());
            customer.setSurname(generateSomeString());
            customer.setPhone("" + generateSomeNumber(10));
            customer.setAddress(generateSomeString());

            ServiceDataSource.getInstance().persist(customer);
        }
    }

    private static int generateSomeNumber(int size) {
        return new Random().nextInt(size);
    }

    private static void generateDetails(int count) {
        for (int i = 0; i < count; i++) {
            Detail detail = new Detail();
            detail.setName(generateSomeString());
            detail.setDescription(generateSomeString());

            ServiceDataSource.getInstance().persist(detail);
        }
    }

    private static String generateSomeString() {
        Random random = new Random();

        int aCh = 'a';
        StringBuilder builder = new StringBuilder();
        int wordSize = Math.abs(random.nextInt()) % 10 + 5;
        for (int i = 0; i < wordSize; i++) {
            builder.append((char) (aCh + random.nextInt() % 10 + 10));
        }

        return builder.toString();
    }

    private static void initAdministrator() {
        ServiceDataSource.getInstance().cleanAllByClass(UsedDetail.class);
        ServiceDataSource.getInstance().cleanAllByClass(CountedDetail.class);
        ServiceDataSource.getInstance().cleanAllByClass(BrokenDetail.class);
        ServiceDataSource.getInstance().cleanAllByClass(Service.class);
        ServiceDataSource.getInstance().cleanAllByClass(Detail.class);
        ServiceDataSource.getInstance().cleanAllByClass(Customer.class);
        ServiceDataSource.getInstance().cleanAllByClass(SystemUser.class);
        ServiceDataSource.getInstance().cleanAllByClass(Property.class);

        SystemUser admin = new SystemUser();
        admin.setAdmin(true);
        admin.setLastLogin(new Date());
        admin.setLogin("God");
        admin.setPassword("73bcaaa458bff0d27989ed331b68b64d");

        Property property = new Property();
        property.setKey("ORDER_ID");
        property.setVal("1");

        ServiceDataSource.getInstance().persist(admin);
        ServiceDataSource.getInstance().persist(property);
    }
}